package com.bookstore.service.impl;

import com.bookstore.entity.Book;
import com.bookstore.entity.PageBean;
import com.bookstore.entity.User;
import com.bookstore.mapper.UserMapper;
import com.bookstore.service.UserService;
import com.bookstore.tools.CreateSqlSession;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/4/14 14:39
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> selectUserToMessage() {
        List<User> userstomsg = userMapper.selectUserToMessage();
        return userstomsg;
    }

    //用户登录
    @Override
    public User findUser(String username, String password) {
        //拿到一个mybatis操作对象s
        SqlSession sqlSession=CreateSqlSession.getSession();
        //接口指向实现类的对象
        UserMapper um=sqlSession.getMapper(UserMapper.class);
        return um.findByNameAndPass(username,password);
    }

    //查询所有用户
    @Override
    public List<User> findAllUser() {
        //拿到一个mybatis操作对象
        SqlSession sqlSession=CreateSqlSession.getSession();
        //接口指向实现类的对象
        UserMapper um=sqlSession.getMapper(UserMapper.class);
        return um.findAll();
    }

    @Override
    public User findUserByName(String username) {
        User UserbyName = userMapper.findByName(username);
        return UserbyName;
    }

    @Override
    public Integer updatePass(String username, String password) {
        Integer integer = userMapper.updatePass(username, password);
        return integer;
    }

    //用户保存
    @Override
    public void saveUser(String username, String password) {
        //拿到一个mybatis操作对象
        SqlSession sqlSession=CreateSqlSession.getSession();
        //接口指向实现类的对象
        UserMapper um=sqlSession.getMapper(UserMapper.class);
        um.insertUser(username,password);
    }

    @Override
    public PageBean<User> selectByPageUser(int currentPage, int pageSize) {
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        //5. 查询当前页数据
        List<User> rows = userMapper.selectByPageUser(begin, size);
        //6. 查询总记录数
        System.out.println(rows);
        int totalCount = userMapper.selectTotalCount();
        //7. 封装PageBean对象
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }
}
